# Maintainer: Dave Murphy <davem@devkitpro.org>
# Contributor: averne <averne381@gmail.com>
# Contributor: Ezekiel Bethel <stary@9net.org>
# Contributor: carstene1ns <dev f4ke de>
# Contributor: jakibaki <jakibaki live com>

pkgname=switch-ffmpeg
pkgver=6.1
commit=ff3429991ec1bac1d1b71215402e3d195162e125
pkgrel=2
pkgdesc='ffmpeg port (for Nintendo Switch homebrew development)'
arch=('any')
url='https://ffmpeg.org/'
license=('LGPL' 'GPL', 'GPL3')
options=(!strip staticlibs)
makedepends=('switch-pkg-config' 'dkp-toolchain-vars')
depends=('switch-zlib' 'switch-bzip2' 'switch-libass' 'switch-libfribidi'
         'switch-freetype' 'switch-harfbuzz' 'switch-mbedtls' 'switch-dav1d')
source=("https://github.com/FFmpeg/FFmpeg/archive/${commit}.tar.gz" "ffmpeg.patch" "network.patch" "ffmpeg_bad_frame.patch")
sha256sums=(
 'SKIP'
 'SKIP'
 'SKIP'
 'SKIP'
)
groups=('switch-portlibs')

prepare() {
  cd FFmpeg-$commit

  rm -rf libavutil/hwcontext_tx1.c
  rm -rf libavutil/hwcontext_tx1.h
  rm -rf libavutil/nvdec_drv.h
  rm -rf libavutil/nvhost_ioctl.h
  rm -rf libavutil/nvjpg_drv.h
  rm -rf libavutil/nvmap_ioctl.h
  rm -rf libavutil/tx1.c
  rm -rf libavutil/tx1.h
  rm -rf libavutil/tx1_host1x.h
  rm -rf libavutil/vic_drv.h
  rm -rf libavutil/clb0b6.h
  rm -rf libavutil/clc5b0.h
  rm -rf libavutil/cle7d0.h
  rm -rf libavcodec/tx1_*

  patch -Np1 -i "$srcdir/ffmpeg.patch"
  patch -Np1 -i "$srcdir/network.patch"
  patch -Np1 -i "$srcdir/ffmpeg_bad_frame.patch"
}

build() {
  cd FFmpeg-$commit

  source /opt/devkitpro/switchvars.sh

  ./configure --prefix=$PORTLIBS_PREFIX --enable-gpl --disable-shared --enable-static \
    --cross-prefix=aarch64-none-elf- --enable-cross-compile \
    --arch=aarch64 --cpu=cortex-a57 --target-os=horizon --enable-pic \
    --extra-cflags='-D__SWITCH__ -D_GNU_SOURCE -O2 -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec' \
    --extra-cxxflags='-D__SWITCH__ -D_GNU_SOURCE -O2 -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec' \
    --extra-ldflags='-fPIE -L${PORTLIBS_PREFIX}/lib -L${DEVKITPRO}/libnx/lib' \
    --disable-runtime-cpudetect --disable-programs --disable-debug --disable-doc --disable-autodetect \
    --enable-asm --enable-neon \
    --disable-postproc --disable-avdevice --disable-encoders --disable-muxers \
    --enable-swscale --enable-swresample --enable-network  \
    --disable-protocols --enable-protocol=file,http,tcp,udp,rtmp,hls,https,tls,ftp \
    --enable-zlib --enable-bzlib --enable-libass --enable-libfreetype --enable-libfribidi --enable-libdav1d \
    --enable-gpl --enable-tx1 \
    --enable-version3 --enable-mbedtls

  make -j$(nproc)
}

package() {
  cd FFmpeg-$commit

  source /opt/devkitpro/switchvars.sh

  make DESTDIR="$pkgdir" install

  # remove examples
  rm -r "$pkgdir"${PORTLIBS_PREFIX}/share
}